﻿using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.ServiceProvider;

namespace ZBJF.Warranty.BLL
{
    public partial class ServiceProviderService
    {
        #region 根据主键ID获取服务商 + ServiceProvider LoadByID(int id)
        public ServiceProvider LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断服务商是否存在+bool IsExist(string accessoriestName, int id = 0)
        /// <summary>
        /// 判断服务商是否存在
        /// </summary>
        /// <param name="accessoriestName">服务商名称</param>
        /// <param name="id">服务商ID</param>
        /// <returns></returns>

        public bool IsExist(string serviceProviderName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProvider>();
            if (!string.IsNullOrWhiteSpace(serviceProviderName))
                expressionWhere = expressionWhere.And(o => o.ServiceProviderName.Equals(serviceProviderName));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取服务商列表 + IQueryable<ServiceProvider> GetPagingList(ServiceProviderModel model, out int total)
        /// <summary>
        /// 分页获取服务商列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<ServiceProvider> GetPagingList(ServiceProviderModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProvider>();

            //服务商类型
            if (model.ServiceProviderTypeCode > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ServiceProviderType.Where(t=>t.ServiceProviderTypeCode == model.ServiceProviderTypeCode).Any());
            }

            //服务商名称
            if (!string.IsNullOrEmpty(model.ServiceProviderName))
            {
                string kw = model.ServiceProviderName.ToLower();
                expressionWhere = expressionWhere.And(o => o.ServiceProviderName.ToLower().Contains(kw));
            }

            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<ServiceProvider>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion
    }
}
